//
//  TSCellMoveCollectionView.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 2017/9/11.
//  Copyright © 2017年 dvlproad. All rights reserved.
//

#import "TSCellMoveCollectionView.h"
#import <CJCollectionViewLayout/CQCollectionViewFlowLayout.h>
#import <CJBaseUIKit/UIView+CJShake.h>
#import "TSCellMoveCollectionViewCell.h"

@interface TSCellMoveCollectionView () <UICollectionViewDataSource> {
    
}

@end


@implementation TSCellMoveCollectionView


/// 初始化方法
- (instancetype)init {
    CQCollectionViewFlowLayout *layout = [[CQCollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //以下值必须二选一设置（默认cellWidthFromFixedWidth设置后，另外一个自动失效）
    layout.cellWidthFromPerRowMaxShowCount = 4;
    //layout.cellWidthFromFixedWidth = 50;
    layout.widthHeightRatio = 1.0;
    
    
    self = [super initWithFrame:CGRectZero collectionViewLayout:layout];
    if (self) {
        [self setupConfigure];
    }
    return self;
}
- (void)setupConfigure {
    self.backgroundColor = [UIColor clearColor];
    
    //以下值，可选设置
    //self.allowsMultipleSelection = YES; //是否打开多选
    
    /* 设置Register的Cell或Nib */
    TSCellMoveCollectionViewCell *registerCell = [[TSCellMoveCollectionViewCell alloc] init];
    [self registerClass:[registerCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    /* 设置DataSource */
    self.dataSource = self;
    
    /* 设置Delegate */
    self.delegate = self;
}

#pragma mark - Event
/// 列表开始抖动
- (void)beginShake {
    self.cjShakeType = CJShakeTypeMoving;
    [self reloadData];
}

/// 列表停止抖动
- (void)endShake {
    self.cjShakeType = CJShakeTypeEnd;
    [self reloadData];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger cellCount = self.dataModels.count;
    return cellCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TSCellMoveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSString *dataModel = [self.dataModels objectAtIndex:indexPath.item];
    cell.text = dataModel;
    
    if (collectionView.cjShakeType == CJShakeTypeMoving) {
        [cell cjShakeKeeping];
    }
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}


@end
